#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import unicodecsv as csv
import sys
import string
import codecs
from copy import copy
from hashlib import sha1
import hmac
import records

#file_path = sys.argv[1]
def encode(s):
    if s is string:
        return s.encode('utf-8')
    return ''

data = []

index = 0
with records.Database('mysql+pymysql://root:P@ssw0rd@192.168.10.2:3306/makex_game?charset=utf8') as db:
    db.query('use makex;')

    db.query("SET NAMES 'utf8';")

    sql = "select * from rank;"
    rawrows = db.query(sql)
    rows=json.loads(rawrows.export('json'))




with open('rank.csv', 'w') as out:
    out.write(codecs.BOM_UTF8)
    header = ['teamNo','name', 'teamType', 'points', 'totalScore', 'autoBeatScore', 'autoPatrolScore', 'manualScore', 'finalScore', 'wins', 'defeats',
    'deuces','computedScore', 'order']

    writer = csv.DictWriter(out, header, extrasaction='ignore')
    writer.writeheader()
    writer.writerows(data)





